<template>
  <!-- 选择日期 :placeholder="nowDate"-->
  <div class="conditions">
    <span class="title f-mr10">{{title}}</span>
    <i-select
      v-model="type"
      style="width:50px;margin-right:5px;"
      filterable
      v-if="selectShow"
      placeholder="请输入关键词搜索"
    >
      <i-option v-for="item in options" :key="item.value" :value="item.value">{{ item.label }}</i-option>
    </i-select>

    <Date-picker
      :type="type"
      :editable="false"
      :value="value[0]"
      :format="format"
      @on-change="dateAChange"
      style="width: 200px"
      :clearable="clearable"
      transfer
    ></Date-picker>&#12288;至&#12288;
    <Date-picker
      :type="type"
      :editable="false"
      :value="value[1]"
      :format="format"
      @on-change="dateBChange"
      style="width: 200px"
      :clearable="clearable"
      transfer
    ></Date-picker>
  </div>
</template>

<script>
import util from "@/libs/util.js";
export default {
  props: {
    title: {
      type: String,
      default: "比较周期"
    },
    value: {
      type: Array,
      default: []
    },
    disabled: {
      type: String,
      default: null
    },
    type: {
      type: String,
      default: "month"
    },
    typeUpdate: {
      type: String,
      default: "1"
    },
    selectShow: {
      type: Boolean,
      default: true
    },
    clearable: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      format: "",
      options: [
        {
          label: "年",
          value: "year"
        },
        {
          label: "月",
          value: "month"
        },
        {
          label: "日",
          value: "date"
        },
        {
          label: "时",
          value: "datetime"
        }
      ]
    };
  },
  computed: {},
  watch: {
    type(val) {
      this.format = "";
      if (val == "year") {
        // if (this.value[0] != "") {
        //   this.value[0] = this.value[0].slice(0, 4);
        // }
        // if (this.value[1] != "") {
        //   this.value[1] = this.value[1].slice(0, 4);
        // }
        this.value[0] = String(Number(util.date.getNowFormatDate("year")) - 1);
        this.value[1] = util.date.getNowFormatDate("year");
      } else if (val == "month") {
        this.value[0] = util.date.getNowFormatDate("year") + "-01";
        this.value[1] = util.date.getNowFormatDate("month");
        // if (this.value[0] != "") {
        //   if (
        //     this.value[0].length == 4 &&
        //     this.value[0] == util.date.getNowFormatDate("year") &&
        //     this.value[1] == util.date.getNowFormatDate("year")
        //   ) {
        //     this.value[0] = this.value[0] + "-01";
        //   } else if (this.value[0].length == 4) {
        //     this.value[0] = this.value[0] + "-01";
        //   } else if (this.value[0].length == 10) {
        //     this.value[0] = this.value[0].slice(0, 7);
        //   } else if (this.value[0].length > 10) {
        //     this.value[0] = this.value[0].slice(0, 7);
        //   }
        // }
        // if (
        //   this.value[1].length == 4 &&
        //   this.value[0] == util.date.getNowFormatDate("year") + "-01" &&
        //   this.value[1] == util.date.getNowFormatDate("year")
        // ) {
        //   this.value[1] = util.date.getNowFormatDate("month");
        // } else if (this.value[1] != "") {
        //   if (this.value[1].length == 4) {
        //     this.value[1] = this.value[1] + "-01";
        //   } else if (this.value[1].length == 10) {
        //     this.value[1] = this.value[1].slice(0, 7);
        //   } else if (this.value[1].length > 10) {
        //     this.value[1] = this.value[1].slice(0, 7);
        //   }
        // }
      } else if (val == "date") {
        this.value[0] = util.date.getNowFormatDate("month") + "-01";
        this.value[1] = util.date.getNowFormatDate("date");
        // if (this.value[0] != "") {
        //   if (
        //     this.value[0].length == 4 &&
        //     this.value[0] == util.date.getNowFormatDate("year") &&
        //     this.value[1] == util.date.getNowFormatDate("year")
        //   ) {
        //     this.value[0] = this.value[0] + "-01-01";
        //   } else if (this.value[0].length == 4) {
        //     this.value[0] = this.value[0] + "-01-01";
        //   } else if (this.value[0].length == 7) {
        //     this.value[0] = this.value[0] + "-01";
        //   } else if (this.value[0].length > 10) {
        //     this.value[0] = this.value[0].slice(0, 10);
        //   }
        // }
        // if (this.value[1] != "") {
        //   if (
        //     this.value[1].length == 4 &&
        //     this.value[0] == util.date.getNowFormatDate("year") + "-01-01" &&
        //     this.value[1] == util.date.getNowFormatDate("year")
        //   ) {
        //     this.value[1] = util.date.getNowFormatDate("date");
        //   } else if (this.value[1].length == 4) {
        //     this.value[1] = this.value[1] + "-01-01";
        //   } else if (this.value[1].length == 7) {
        //     this.value[1] = this.value[1] + "-01";
        //   } else if (this.value[1].length > 10) {
        //     this.value[1] = this.value[1].slice(0, 10);
        //   }
        // }
      } else if (val == "datetime") {
        this.format = "yyyy-MM-dd HH:mm";
        this.value[0] = util.date.getNowFormatDate("date") + " 00:00";
        let myDate = new Date();

        let year = myDate.getFullYear();

        let month = myDate.getMonth() + 1;

        let date = myDate.getDate();
        let h = myDate.getHours(); //获取当前小时数(0-23)
        let m = myDate.getMinutes(); //获取当前分钟数(0-59)
        let s = myDate.getSeconds();

        //获取当前时间
        let conver = s => {
          return s < 10 ? "0" + s : s;
        };
        let now =
          year +
          "-" +
          conver(month) +
          "-" +
          conver(date) +
          " " +
          conver(h) +
          ":" +
          conver(m);
        this.value[1] = now;

        // if (this.value[0] != "") {
        //   if (
        //     this.value[0].length == 4 &&
        //     this.value[0] == util.date.getNowFormatDate("year") &&
        //     this.value[1] == util.date.getNowFormatDate("year")
        //   ) {
        //     this.value[0] = this.value[0] + "-01-01 00:00";
        //   } else if (this.value[0].length == 4) {
        //     this.value[0] = this.value[0] + "-01-01 00:00";
        //   } else if (this.value[0].length == 7) {
        //     this.value[0] = this.value[0] + "-01 00:00";
        //   } else if (this.value[0].length == 10) {
        //     this.value[0] = this.value[0] + " 00:00";
        //   }
        // }

        // if (this.value[1] != "") {
        //   if (
        //     this.value[1].length == 4 &&
        //     this.value[0] == util.date.getNowFormatDate("year") + "-01-01 00:00" &&
        //     this.value[1] == util.date.getNowFormatDate("year")
        //   ) {
        //     this.value[1] = util.date.getNowFormatDate("date") + " 00:00";
        //   } else if (this.value[1].length == 4) {
        //     this.value[1] = this.value[1] + "-01-01 00:00";
        //   } else if (this.value[1].length == 7) {
        //     this.value[1] = this.value[1] + "-01 00:00";
        //   } else if (this.value[1].length == 10) {
        //     this.value[1] = this.value[1] + " 00:00";
        //   }
        // }
      }
      this.$emit("input", this.value);
      if (this.typeUpdate == "1") {
        this.$emit("type1Update", val);
      } else if (this.typeUpdate == "2") {
        this.$emit("type2Update", val);
      }
    }
  },
  methods: {
    dateAChange(date) {
      this.value[0] = date;
      if (this.type == "datetime") {
        this.value[0] = date.substr(0, 14) + "00";
      }
      this.$emit("input", this.value);
    },
    dateBChange(date) {
      this.value[1] = date;
      if (this.type == "datetime") {
        this.value[1] = date.substr(0, 14) + "00";
      }
      this.$emit("input", this.value);
    }
  },
  created() {
    if (this.disabled != null) {
      this.options = this.options.filter(item => {
        return item.value != this.disabled;
      });
    }
  },
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
